/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.wordplat.quickstart.slice;

import com.wordplat.quickstart.ResourceTable;
import com.wordplat.quickstart.base.BaseAbilitySlice;
import com.wordplat.quickstart.mvp.StockPresenter;
import com.wordplat.quickstart.slider.PagerProvider;
import com.wordplat.quickstart.wight.PullLayout.pulllistview.RefreshComponent;
import com.wordplat.quickstart.wight.navigation.BottomNavigation;
import com.wordplat.quickstart.wight.navigation.BottomNavigationItem;
import ohos.aafwk.content.Intent;
import ohos.agp.components.PageSlider;

import java.util.ArrayList;

/**
 * With_Fragment_And_TabLayout_Switcher_Example_Slice
 *
 * @since 2021-04-22
 */
public class With_Fragment_And_TabLayout_Switcher_Example_Slice extends BaseAbilitySlice {
    private BottomNavigation mTabLayout;
    private RefreshComponent refreshComponent;
    private final ArrayList<BottomNavigationItem> bottomNavigationItems = new ArrayList<>();
    private PageSlider mkLineContent;
    private PagerProvider dayKLinePager;
    private PagerProvider weekKLinePager;
    private PagerProvider monthKLinePager;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_slice_with_fragment_and_tablayout_switcher);
        initViews();
        refresh();
    }

    private void initViews() {
        mTabLayout = (BottomNavigation) findComponentById(ResourceTable.Id_tabLayout);
        mkLineContent = (PageSlider) findComponentById(ResourceTable.Id_kLineContent);
        refreshComponent = (RefreshComponent) findComponentById(ResourceTable.Id_fragmentRefreshComponent);

        dayKLinePager = PagerProvider.newInstance(this, StockPresenter.KLineType.DAY, refreshComponent);
        weekKLinePager = PagerProvider.newInstance(this, StockPresenter.KLineType.WEEK, refreshComponent);
        monthKLinePager = PagerProvider.newInstance(this, StockPresenter.KLineType.MONTH, refreshComponent);
        mkLineContent.setProvider(dayKLinePager);
        mkLineContent.setProvider(weekKLinePager);
        mkLineContent.setProvider(monthKLinePager);
        mkLineContent.setCurrentPage(0);
        mkLineContent.setSlidingPossible(false);

        dayKLinePager = new PagerProvider(this, StockPresenter.KLineType.DAY);

        mkLineContent.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
            }
        });
    }

    /**
     * refresh
     */
    public void refresh() {
        mTabLayout.removeAllItems();
        mTabLayout.setColored(false);
        mTabLayout.setSelectedBackgroundVisible(false);
        bottomNavigationItems.clear();

        BottomNavigationItem item1 =
                new BottomNavigationItem(
                        "日K", getContext());
        BottomNavigationItem item2 =
                new BottomNavigationItem(
                        "周K", getContext());
        BottomNavigationItem item3 =
                new BottomNavigationItem(
                        "月K", getContext());

        bottomNavigationItems.add(item1);
        bottomNavigationItems.add(item2);
        bottomNavigationItems.add(item3);
        mTabLayout.setSelectHideNotification(false);
        mTabLayout.addItems(bottomNavigationItems);

        setListener();
    }

    private void setListener() {
        mTabLayout.setOnTabSelectedListener(
                new BottomNavigation.OnTabSelectedListener() {
                    @Override
                    public boolean onTabSelected(int position, boolean wasSelected) {
                        mkLineContent.setCurrentPage(position);
                        switch (position) {
                            case 0:
                                dayKLinePager.loadData();
                                break;
                            case 1:
                                weekKLinePager.loadData();
                                break;
                            case 2:
                                monthKLinePager.loadData();
                                break;
                            default:
                                break;
                        }
                        return true;
                    }
                });
    }
}
